/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdminUsers.java
 *
 * Created on 30.05.2009, 12:49:40
 */

package Tabs;

import Binding.Binder;
import Binding.GreaterThenRule;
import Binding.RequiredRule;
import BusinessLayer.BusinessLayer;
import Objekte.User;
import Objekte.UserListModel;
import dialogs.ChangePassword;
import dialogs.Register;
import logging.Logger;
import wisi.WiSiApp;
import wisi.WiSiView;

/**
 *
 * @author benji
 */
public class AdminUsers extends javax.swing.JPanel {

    private Binder binder = new Binder();
    /** Creates new form AdminUsers */
    private UserListModel user;

    public AdminUsers() {
        initComponents();

        Logger.trace("Lade " + this.getName());

        user = new UserListModel();
        this.jTable1.setModel(user);
    }

    /**
     * userliste laden
     */
     public void BindTo() {
        try{
            if(!BusinessLayer.isLoggedIn())return;
            if(!BusinessLayer.isAdmin(BusinessLayer.getUserId()))return;
            user.setData(BusinessLayer.getUserList());
            Logger.trace("BindTo");
        }
        catch(Exception e){
            Logger.error("BindTo - UserTable", e);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        idLabel = new javax.swing.JLabel();
        nameTextField = new javax.swing.JTextField();
        guthabenTextField = new javax.swing.JTextField();
        changePasswordButton = new javax.swing.JButton();
        adminCheckBox = new javax.swing.JCheckBox();
        jLabel6 = new javax.swing.JLabel();
        saveButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        rankLabel = new javax.swing.JLabel();
        newPersonButton = new javax.swing.JButton();

        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.setName("jPanel1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(wisi.WiSiApp.class).getContext().getResourceMap(AdminUsers.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        idLabel.setText(resourceMap.getString("idLabel.text")); // NOI18N
        idLabel.setName("idLabel"); // NOI18N

        nameTextField.setText(resourceMap.getString("nameTextField.text")); // NOI18N
        nameTextField.setName("nameTextField"); // NOI18N

        guthabenTextField.setText(resourceMap.getString("guthabenTextField.text")); // NOI18N
        guthabenTextField.setName("guthabenTextField"); // NOI18N

        changePasswordButton.setText(resourceMap.getString("changePasswordButton.text")); // NOI18N
        changePasswordButton.setName("changePasswordButton"); // NOI18N
        changePasswordButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                changePasswordButtonActionPerformed(evt);
            }
        });

        adminCheckBox.setText(resourceMap.getString("adminCheckBox.text")); // NOI18N
        adminCheckBox.setName("adminCheckBox"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        saveButton.setText(resourceMap.getString("saveButton.text")); // NOI18N
        saveButton.setName("saveButton"); // NOI18N
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        deleteButton.setText(resourceMap.getString("deleteButton.text")); // NOI18N
        deleteButton.setName("deleteButton"); // NOI18N
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        rankLabel.setText(resourceMap.getString("rankLabel.text")); // NOI18N
        rankLabel.setName("rankLabel"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(guthabenTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(idLabel))
                    .addComponent(adminCheckBox)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(rankLabel))
                    .addComponent(saveButton, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(deleteButton, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel3))
                        .addGap(14, 14, 14)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(changePasswordButton)
                            .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(idLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(changePasswordButton, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(guthabenTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(rankLabel))
                .addGap(12, 12, 12)
                .addComponent(adminCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(saveButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(deleteButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        newPersonButton.setText(resourceMap.getString("newPersonButton.text")); // NOI18N
        newPersonButton.setName("newPersonButton"); // NOI18N
        newPersonButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newPersonButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(newPersonButton))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
                        .addComponent(newPersonButton))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    /**
     * User in der DetailView anzeigen
     * @param evt
     */
    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        User u = user.getRow(this.jTable1.getSelectedRow());
        Logger.debug("Lade User für Detail View");

        binder.BindTo(idLabel, u.getId());
        binder.BindTo(nameTextField, u.getName());
        binder.BindTo(guthabenTextField, u.getGuthaben());
        binder.BindTo(rankLabel, u.getRankString());
        if(u.isIsAdmin()){
            this.adminCheckBox.setSelected(true);
            Logger.trace("Setzte Admin Flag");
        }
        else{
            this.adminCheckBox.setSelected(false);
        }
    }//GEN-LAST:event_jTable1MouseClicked

    /**
     * Änderungen beim User speichern
     * @param evt
     */
    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        User u = new User();
        
        RequiredRule rRule = new RequiredRule();
        GreaterThenRule gRule = new GreaterThenRule(0);

        binder.clearErrorList();
        Logger.debug("Clear Error List - binder");

        u.setId(binder.BindFromInt(idLabel, rRule, gRule));
        u.setName(binder.BindFromString(nameTextField, rRule));
        u.setGuthaben(binder.BindFromInt(guthabenTextField, rRule, gRule));
        u.setIsAdmin(this.adminCheckBox.isSelected());


        if(!binder.getHasErrors())
            BusinessLayer.updateUser(u);
            Logger.debug("Update User als Admin");
            WiSiView.refreshAll();
            Logger.trace("Refresh All from " + this.getName());
    }//GEN-LAST:event_saveButtonActionPerformed

    /**
     * User löschen
     * @param evt
     */
    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        User u = new User();
        RequiredRule rRule = new RequiredRule();
        GreaterThenRule gRule = new GreaterThenRule(0);
        binder.clearErrorList();
        Logger.debug("Clear Error List - binder");

        u.setId(binder.BindFromInt(idLabel, rRule, gRule));
        u.setName(binder.BindFromString(nameTextField, rRule));
        u.setGuthaben(binder.BindFromInt(guthabenTextField, rRule, gRule));
        //u.setRank(binder.BindFromInt(rankTextField, rRule, gRule));
        u.setIsAdmin(this.adminCheckBox.isSelected());

        BusinessLayer.deleteUser(u);
        Logger.info("Lösche User: " + u.getName() );
        BindTo();
    }//GEN-LAST:event_deleteButtonActionPerformed

    /**
     * öffnet eine Dialog bei dem der Admin einen neuen User anlegen kann
     * @param evt
     */
    private void newPersonButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newPersonButtonActionPerformed
        Register reg = new Register(WiSiApp.getApplication().getMainFrame(), true);
        reg.setVisible(true);
        BindTo();
    }//GEN-LAST:event_newPersonButtonActionPerformed

    /**
     * der Admin kann das Passwort eines Users löschen
     * muss dazu allerdings das PAsswort des Users kennen
     * @param evt
     */
    private void changePasswordButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changePasswordButtonActionPerformed
        ChangePassword cp = new ChangePassword(WiSiApp.getApplication().getMainFrame(), true);
        cp.setVisible(true);
        BindTo();
    }//GEN-LAST:event_changePasswordButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox adminCheckBox;
    private javax.swing.JButton changePasswordButton;
    private javax.swing.JButton deleteButton;
    private javax.swing.JTextField guthabenTextField;
    private javax.swing.JLabel idLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JButton newPersonButton;
    private javax.swing.JLabel rankLabel;
    private javax.swing.JButton saveButton;
    // End of variables declaration//GEN-END:variables

}
